package com.gudy.engine.handler.risk;

import com.gudy.engine.bean.command.CmdResultCode;
import com.gudy.engine.bean.command.RbCmd;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.eclipse.collections.api.set.primitive.MutableIntSet;
import org.eclipse.collections.api.set.primitive.MutableLongSet;
import thirdpart.order.CmdType;

@Log4j2
@RequiredArgsConstructor
public class ExistRiskHandler extends BaseHandler{

    @NonNull
    private MutableLongSet uidSet;

    @NonNull
    private MutableIntSet codeSet;

    //发布行情Event  新委托Event  撤单Event
    //权限控制  系统关机
    @Override
    public void onEvent(RbCmd cmd, long sequence, boolean endOfBatch) throws Exception {
        //如果指令是系统自己产生的  就不用
        if(cmd.command == CmdType.HQ_PUB){return;}
        if(cmd.command == CmdType.NEW_ORDER || cmd.command == CmdType.CANCEL_ORDER){
            //1 用户是否存在
            if(!uidSet.contains(cmd.uid)){
                log.error("illegaluid[{}]】", cmd.uid);
                cmd.resultCode = CmdResultCode.RISK_INVALID_USER;
                return;
            }
            //2 股票是否合法
            if(!codeSet.contains(cmd.code)){
                log.error("contains", cmd.uid);
                cmd.resultCode = CmdResultCode.RISK_INVALID_CODE;
                return;
            }
        }

    }
}
